import { isReferee } from "@/func";
import { KcpdFuncs } from "@/kcpd/func";
import { POFChooseRoomForm } from "@/pof/chooseRoom";
import { WereWolfGame } from "@/werewolf/game";
import { WereWolfMaps } from "@/werewolf/maps";
import { WereWolfPickNumberState } from "@/werewolf/pickNumberState/state";
import { Dimensions } from "@sapi-game/constants";
import { Game } from "@sapi-game/main";
import { Duration } from "@sapi-game/utils";
import { formManager } from "sapi-pro";
import { getAllPlayers } from "sapi-pro/scripts/func";
import { HubGameData } from "./components/hubGames";

export const HubGameList: HubGameData[] = [
    //你的跑酷我来造
    {
        entityData: {
            entityId: "game:parkourhub",
            animationName: "animation.parkourhub",
            interval: new Duration(118),
        },
        onInteract(p) {
            p.teleport(
                { x: 721, y: -59, z: 435 },
                { dimension: Dimensions.Overworld }
            );
        },
    },
    //电梯猫
    {
        entityData: {
            entityId: "partygames:elevatorhub",
            animationName: "animation.elevator.hub",
            interval: Duration.fromSeconds(4),
        },
        onInteract(p) {
            p.teleport(
                { x: 369.0, y: -59.0, z: 159.0 },
                { rotation: { x: 0, y: 180 } }
            );
        },
    },
    //色盲派对
    {
        entityData: {
            entityId: "game:smpdhub",
            animationName: "animation.smpdhub",
            interval: Duration.fromSeconds(8),
        },
        onInteract() {
            //tp管理员
            Game.playerManager
                .getFreePlayers()
                .forEach((p) =>
                    p.teleport(
                        isReferee(p)
                            ? { x: 186, y: -55, z: -199 }
                            : { x: 146, y: -59, z: -165 }
                    )
                );
        },
        requirePerm: true,
    },
    //大富翁
    {
        buttonLoc: [414, 3, 449],
        onInteract(p) {
            p.teleport({ x: -189, y: -37, z: 258 });
        },
    },
    //狼人杀
    {
        buttonLoc: [439, 3, 459],
        onInteract: (p) => {
            const game = Game.manager.getGame(WereWolfGame, "classic");
            if (game) {
                const state = game.getState(WereWolfPickNumberState);
                if (state) {
                    state.join(p);
                } else {
                    p.sendMessage("游戏已开始");
                }
                return;
            }
            Game.manager.startGame(
                WereWolfGame,
                { map: WereWolfMaps.classic },
                "classic"
            );
        },
    },
    //狼人杀（营地地图）
    {
        buttonLoc: [449, 3, 459],
        onInteract: (p) => {
            const game = Game.manager.getGame(WereWolfGame, "camp");
            if (game) {
                const state = game.getState(WereWolfPickNumberState);
                if (state) {
                    state.join(p);
                } else {
                    p.sendMessage("游戏已开始");
                }
                return;
            }
            Game.manager.startGame(
                WereWolfGame,
                { map: WereWolfMaps.camp },
                "camp"
            );
        },
    },
    //脏鱼游戏
    {
        buttonLoc: [452, 3, 431],
        onInteract() {
            getAllPlayers().forEach((p) => {
                p.teleport(
                    { x: -156.5, y: -59.0, z: -58.5 },
                    { rotation: { x: 0, y: 90 } }
                );
            });
        },
        requirePerm: true,
    },
    //烤肠派对
    {
        buttonLoc: [451, 3, 466],
        onInteract(p) {
            KcpdFuncs.hub(p);
        },
    },
    //鸡王争霸
    {
        entityData: {
            entityId: "game:jwzbhub",
            animationName: "animation.jwzbhub",
            interval: new Duration(40),
        },
        onInteract(p) {
            p.teleport(
                { x: 34.5, y: -60.0, z: 52.5 },
                { rotation: { x: 0, y: 0 } }
            );
        },
    },
    //幸运之柱
    {
        entityData: {
            entityId: "game:pofhub",
            animationName: "animation.pofhub",
            interval: Duration.fromSeconds(5),
        },
        onInteract(p) {
            formManager.open(p, POFChooseRoomForm);
        },
    },
    //惊天旷工团
    {
        buttonLoc: [439, 3, 431],
        onInteract(p) {
            p.teleport({ x: 868, y: -52.0, z: 944.61 });
        },
    },
    //泥土商店
    {
        buttonLoc: [440, 2, 470],
        onInteract(p) {
            p.teleport({ x: 1914.65, y: -60.0, z: 1901.16 });
        },
    },
];
